import "../css/Login.scss"
import { Link } from 'react-router-dom';
import { Toast } from 'antd-mobile';
import { getLogin } from './../action/action';
import { connect } from "react-redux";
import { setCookie, getCookie } from '../js/cookie';
@connect()
export class Login extends React.Component {
    login() {
        let { dispatch } = this.props;
        let tel = this.refs.tel.value;
        let pwd = this.refs.pwd.value;
        if (!(/^1[34578]\d{9}$/.test(tel))) {
            Toast.info('电话号码有误 !!!', 2, null, false);
        } else if (pwd == "") {
            Toast.info('密码不能为空 !!!', 2, null, false);
        } else {
            dispatch(getLogin(tel, pwd, (data) => {
                if (data.code == 1) {
                    Toast.success(data.msg, 1);
                    setCookie("ky_uid", data.uid, 7);
                    this.props.history.push(getCookie("ky_history_url"));
                } else {
                    Toast.fail(data.msg, 1);
                }
            }));
        }
    }
    render() {
        return (
            <div className="login">
                <header>
                    <div>
                        <i className="iconfont icon-chahao" onClick={() => this.props.history.push(getCookie("ky_history_url"))}></i>
                    </div>
                </header>
                <main>
                    <h3>开眼</h3>
                    <div>
                        <div>
                            <i className="iconfont icon-yonghu"></i>
                            <input type="tel" ref="tel" placeholder="请输入手机号" />
                        </div>
                        <div>
                            <i className="iconfont icon-mima"></i>
                            <input type="password" ref="pwd" placeholder="请输入密码" />
                        </div>
                    </div>
                    <button onClick={() => this.login()}>登录</button>
                    <Link to={"/register"}><span>用户注册</span></Link>
                </main>
            </div>
        )
    }
}
